<?php

namespace app\console\controller;

use think\Db;
use think\Url;
use think\Request;
use think\Controller;
use app\console\model\MemberRefund as MemberRefundModel;
use app\console\model\MemberOrderAttached as MemberOrderAttachedModel;

class MemberRefund extends Controller
{
    public function index()
    {
        $order = Db::name('order')->where('order_number','201812080946402021267629')->find();//订单所有数据
        $manage_id = Db::name('share_member')->where('member_id',$order['member_id'])->value('manage_id');//获得直属业务员

        $salesman_array = parentPath($manage_id,'salesman',true);//当前业务员上级

//  array(3) {
//        [0] => array(3) {
//            ["id"] => int(7)
//            ["manage_id"] => int(6)
//            ["parent_id"] => int(0)
//  }
//  [1] => array(3) {
//            ["id"] => int(5)
//            ["manage_id"] => int(27)
//            ["parent_id"] => int(6)
//  }
//  [2] => array(3) {
//            ["id"] => int(6)
//            ["manage_id"] => int(28)
//            ["parent_id"] => int(27)
//  }
//}

        $salesman_list = array_reverse($salesman_array);
         //array_slice($salesman_array,0,1);

        if($salesman_list){
            $pre = 0;
            foreach ($salesman_list as $k => $v){
                if($k == 0) break;
                    echo $k;
//                if($k == 0)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
//                if($k == 1)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
//                if($k == 2)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
//                $insert = [
//                    'order_number' => $order['order_number'],
//                    'manage_id' => $v['manage_id'],
//                    'order_type' => $order['order_type'],
//                    'member_id' => $order['member_id'],
//                    'amount' => $order['amount'],
//                    'price' => $order['amount']*$pre/100,
//                    'create_time' => $order['create_time']
//                ];

                //Db::name('member_distribution_records')->insert($insert);
            }
        }
    }

    public function abc(){
        $order = Db::name('order')->where('order_number','201812120934352958228716')->where('status','neq',1)->find();//订单所有数据
        $manage_id = Db::name('share_member')->where('member_id',$order['member_id'])->value('manage_id');//获得直属业务员
        $salesman_array = parentPath($manage_id,'salesman',true);//当前业务员上级
        $salesman_list = array_reverse($salesman_array);
        //$salesman_list = array_slice($salesman_array,0,3);
        if($salesman_list){

            $pre = 0;
            foreach ($salesman_list as $k => $v){
                if($k > 2) break;
                if($k == 0)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
                if($k == 1)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
                if($k == 2)  $pre = Db::name('config')->where('varname','web_d'.($k+1))->value('varvalue');
                $insert = [
                    'order_number' => $order['order_number'],
                    'order_type' => $order['order_type'],
                    'member_id' => $order['member_id'],
                    'manage_id' => $v['manage_id'],
                    'amount' => $order['amount'],
                    'price' => $order['amount']*$pre/100,
                    'create_time' => $order['create_time']
                ];
                //Log::DEBUG('insert'.json_encode($insert));
                $m_ret = Db::name('member_distribution_records')->insert($insert);
                //Log::DEBUG('member_distribution_records'.$m_ret);
            }
        }
    }


    /*
    * 订单审核
    */
    public function examine($id)
    {
        if (Request::instance()->isPost()) {

            $data = Request::instance()->param();
            if ($data['order_status'] == 10) {
                $data['status'] = 1;
                $data['refund_time'] = time();
            }else if ($data['order_status'] == 11){
                $data['status'] = 2;
            }else{
                $data['status'] = 3;
            }

            // 提交数据
            $dispose = (new MemberRefundModel())->allowField(true)->save($data, ['id' => $id]);

            if ($dispose || $dispose == 0) {
                win('操作成功', Url::build('MemberRefund/index'));
            }

            fail('操作失败');

        }

        $item = Db::view('member_order', '*')
            ->view('shop', 'member_id as user_id,title', 'shop.member_id = member_order.shop_id')
            ->view('member', 'id as user_id,username', 'member.id = shop.member_id')
            ->where(['member_order.id' => $id])
            ->order(['create_time' => 'desc'])
            ->find();
        $item['buyers'] = Db::name('member')->where('id', $item['member_id'])->value('username');


        $goods_list = (new MemberOrderAttachedModel)->where(['order_id' => $id])->select();


        $refund_find = (new MemberRefundModel)->where(['order_number' => $item['shop_order_number']])->find();


        return $this->fetch('', [
            "mate_title"  => '订单详情',
            'item'        => $item,
            'goods_list'  => $goods_list,
            'refund_find' => $refund_find,

        ]);
    }
}